<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span
          class="iconfont iconnew new"
          @click="$router.push({ path: '/index' })"
        ></span>
      </div>
      <span @click="attention" :class="{ att: article.has_follow }">{{
        article.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ article.title }}</div>
      <div class="desc">
        <span>{{ article.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ article.create_date | dataForm }}</span>
      </div>
      <div
        class="content"
        v-html="article.content"
        v-if="article.type == 1"
      ></div>
      <!-- 如果数据的type 等于2 就是视频 需要添加视频播放器 -->
      <video
        v-if="article.type == 2"
        controls
        src="http://www.iqiyi.com/v_19ry62q9qo.html"
        poster="https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3225511008,2255649025&fm=26&gp=0.jpg"
      ></video>

      <div class="opt">
        <span :class="{ like: article.like_length }" @click="like">
          <van-icon name="good-job-o" />{{ article.like_length }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img :src="'http://127.0.0.1:3000' + article.user.head_img" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <!-- 底部 -->
    <articleBottom :post="article" @click="collect"></articleBottom>
  </div>
</template>

<script>
import { particulars, post_star } from "@/apis/article.js";
import { dataForm } from "@/utils/myfilter.js";
import articleBottom from "@/components/articlebottom.vue";
import { user_follows, user_unfollow, post_like } from "@/apis/user.js";
export default {
  components: {
    articleBottom,
  },
  data() {
    return {
      article: {
        user: "",
      },
    };
  },

  methods: {
    // 收藏文章
    async collect() {
      let res = await post_star(this.article.id);
      console.log(res);
      if (res.data.message == "收藏成功") {
        this.$toast.success(res.data.message);
      } else {
        this.$toast.fail(res.data.message);
      }
      this.article.has_star = !this.article.has_star;
    },

    // 点赞
    async like() {
      let res = await post_like(this.article.id);
      console.log(res);
      if (res.data.message == "点赞成功") {
        this.article.like_length++;
      } else {
        this.article.like_length--;
      }
    },

    // 点击关注 关键是根据当前的状态值进行处理 ，如果has_follow 是true，则说明当前应景关注了，那么这次的单击是取消关注
    async attention() {
      let res;
      if (this.article.has_follow) {
        // 取消关注
        res = await user_unfollow(this.article.user.id);
      } else {
        // 关注
        res = await user_follows(this.article.user.id);
      }
      this.article.has_follow = !this.article.has_follow;
    },
  },
  // 注册过滤器
  filters: {
    dataForm,
  },
  async mounted() {
    // 根据id获取文章的详情，实现文章详情的动态渲染
    let res = await particulars(this.$route.params.id);
    // console.log(res);
    this.article = res.data.data;
  },
};
</script>

<style lang='less' scoped>
video {
  width: 100%;
}
.comment {
  width: 100vw;
  position: fixed;
  left: 0;
  bottom: -10px;
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
  .att {
    border: 1px solid #ccc;
    background-color: transparent;
    color: #000;
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
      display: block;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like {
    color: red;
  }
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px 40px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>
